package com.coderxq.blog.web.service.user.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.coderxq.blog.web.dao.RoleMapper;
import com.coderxq.blog.web.dao.UserMapper;
import com.coderxq.blog.web.entity.dto.UserRoleParam;
import com.coderxq.blog.web.entity.pojo.Menu;
import com.coderxq.blog.web.entity.pojo.User;
import com.coderxq.blog.web.entity.pojo.UserRoleMenu;
import com.coderxq.blog.web.entity.vo.MenuInfo;
import com.coderxq.blog.web.entity.vo.RoleInfoVo;
import com.coderxq.blog.web.entity.vo.UserInfo;
import com.coderxq.blog.web.service.menu.MenuService;
import com.coderxq.blog.web.service.menu.impl.MenuServiceImpl;
import com.coderxq.blog.web.service.user.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: lijingming
 * @Date: 2025/2/12
 * @Description:
 **/
@Component
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    MenuServiceImpl menuService;

    @Override
    public Integer insertUser(User user){
        String snowflakeId = IdUtil.getSnowflakeNextIdStr();
        user.setUserId(snowflakeId);
        return userMapper.insertUser(user);
    };

    @Override
    public Integer insertUsers(ArrayList<User> users){
        for (User user : users){
            user.setUserId( IdUtil.getSnowflakeNextIdStr());
        }
        return userMapper.insertUsers(users);
    };

    @Override
    public void updateUser(UserInfo userinfo){

        //重置用户角色信息
        if(!userinfo.getRoles().isEmpty()){
            userMapper.deleteRoleByUserId(userinfo.getUserId());
            roleMapper.insertUserRoles(userinfo.getRoles(), userinfo.getUserId());
        }
        userMapper.updateUser(userinfo);

    };

    @Override
    public UserInfo getUserByAccount(String account){
        return userMapper.findUserByAccount(account);
    };

    @Override
    public   PageInfo<User>  getAllUser(int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        ArrayList<User> users = userMapper.findAllUser();
        return  new PageInfo<>(users);
    }

    @Override
    public Integer insertUserRole(UserRoleParam insertUserRoleParam){
        return userMapper.insertUserRole(insertUserRoleParam);
    }

    @Override
    public UserRoleMenu getUserRoleMenu(String userId){
        UserRoleMenu userRoleMenu = userMapper.findUserRoleMenu(userId);

        Map<String,Menu> AllMenu= new HashMap<>();
        ArrayList<RoleInfoVo> RoleList = userRoleMenu.getRoleList();
        for (RoleInfoVo roleInfo : RoleList){
            ArrayList<Menu> menuList = roleInfo.getMenuList();
            for(Menu menu : menuList){
                if (AllMenu.containsKey(menu.getMenuId())){
                    continue;
                }else {
                    AllMenu.put(menu.getMenuId(),menu);
                }
            }
        }

       ArrayList<MenuInfo> menuInfos =  menuService.formatMenu( new ArrayList<>(AllMenu.values()));
        userRoleMenu.setMenuList(menuInfos);
        return userRoleMenu;
    }

    @Override
    public UserInfo getUserByUserId(String userId){
        UserInfo userInfo = userMapper.getUserInfoByUserId(userId);
        return userInfo;
    }
}
